<template>
    <div class="wrapper">
        <img class="wrapper_img" src="http://www.dell-lee.com/imgs/vue3/user.png" alt="">
        <div class="wrapper_input">
            <input type="wrapper_input_content" class="wrapper_input_content"
            v-model="username" placeholder="请输入用户名">
        </div>
        <div class="wrapper_input">
            <input type="password" 
            v-model="password" class="wrapper_input_content" placeholder="请输入密码">
        </div>
        <div class="wrapper_login_botton" @click="handleLogin">登录</div>
        <div class="wrapper_login_link" @click="handleRegisterClick" >立即注册</div>
        <Toast v-if="show" :message="toastMessage"/>
    </div>
    <Toast v-if="show" :message="toastMessage"/>
</template>

<script>
import { useRouter } from 'vue-router'
import { post } from '../../untils/request'
import { reactive, toRefs } from '@vue/reactivity'
import Toast,{ useToastEffect } from '../../components/Toast.vue'
//登录提示框的逻辑
  const useLoginEffect = (showToast) => {
     const router = useRouter()
     const datalist = reactive({
            username: '',
            password: '',
     })
//登录的逻辑
     const handleLogin = async () => {
            try {
                const { username,password } = datalist
                if( !username.trim() || !password.trim() ){
                    showToast('请输入用户名或密码')
                }else{
                    const result = await post('/api/user/login', {
                    username: datalist.username,
                    password: datalist.password
                })
                if (result?.errno === 0) {
                    localStorage.isLogin = true
                    router.push({ name: 'Home' })
                } else {
                    showToast('登陆失败')
                }
                }
                } catch (e) {
                console.log(e);
                showToast('请求失败')
            }
        }
     const { username, password } = toRefs(datalist)
     return { username, password, handleLogin }
}
//切换注册页面的逻辑
    const useRegisterEffect = () => {
        const router = useRouter()
        const handleRegisterClick = () => {
            router.push({ name: 'Register' })
        }
        return { handleRegisterClick }
    }

export default {
    name: 'Login',
    components:{
        Toast
    },
    //让setup更加直白简洁，告诉具体流程
    setup() {
        const { show, toastMessage, showToast } = useToastEffect()
        const { username, password, handleLogin} = useLoginEffect(showToast)  
        const { handleRegisterClick } = useRegisterEffect()
        return {
            handleLogin,handleRegisterClick,toastMessage,show,username, password
        }

    }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables';
.wrapper{
    position:absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    &_img{
        display: block;
        margin: 0 auto .4rem auto;
        width: .66rem;
        height: .66rem;
    }
    &_input{
        box-sizing: border-box;
        height: .48rem;
        margin: 0 .4rem .16rem .4rem;
        padding: 0 .16rem;
        background: #f9f9f9;
        border: 1px solid rgba(0,0,0,0.10);
        border-radius: 6px;
        &_content{
            margin-top: .12rem;
            line-height: .22rem;
            border: none;
            outline: none;
            width: 100%;
            background: none;
            font-size: .16rem;
            color: rgba(0,0,0,0.50);
            &::placeholder{
                color: rgba(0,0,0,0.50);

            }
        }
    }
    &_login_botton{
        margin: .32rem .4rem .16rem .4rem;
        line-height: .48rem;
        background: #0091FF;
        box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
        border-radius: .04rem;
        color: $bgColor;
        font-size: .16rem;
        text-align: center;
    }
    &_login_link {
        text-align: center;
        font-size: .14rem;
        color: $content-notice-fontcolor;
    } 
}
</style>